<template>
  <div class="login">
    <el-card class="login-card">
      <h1 style="color: white;">欢迎</h1>
      <br>
      <el-form :model="ruleForm"
               status-icon
               :rules="rules"
               ref="ruleForm"
               class="demo-ruleForm">

        <el-form-item prop="email">
          <el-input type="text"
                    v-model="ruleForm.email"
                    autocomplete="off">
            <template slot="prepend">
              <div class="input_label">输入邮箱</div>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-input v-model="ruleForm.code">
            <el-button type="primary"
                       style="color: white;background-color: #409EFF  ;"
                       @click="sendCode()"
                       slot="append">{{ numberCodeBtnValue }}</el-button>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input type="password"
                    v-model="ruleForm.password"
                    autocomplete="off"
                    show-password>
            <template slot="prepend">
              <div class="input_label">输入密码</div>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password1">
          <el-input type="password"
                    v-model="ruleForm.password1"
                    autocomplete="off"
                    show-password>
            <template slot="prepend">
              <div class="input_label">确认密码</div>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <div style="display: flex;">
            <!-- <el-checkbox-group v-model="remember">
              <el-checkbox label="记住我"
                           name="type"></el-checkbox>
            </el-checkbox-group> -->
            <div style="margin-left: 310px;"><a href="/login">去登录</a></div>
          </div>

        </el-form-item>

        <el-form-item>
          <el-button type="primary"
                     @click="submitForm('ruleForm')">注册</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>

      </el-form>

    </el-card>
  </div>
</template>
<script>
import api from '@/api/apis/user.js'
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      numberCodeBtnValue: '验证码',
      remember: true,
      ruleForm: {
        email: '',
        code: '',
        password: '',
        password1: '',
        type: 'email',
      },
      rules: {
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
        ],
        password1: [
          {
            validator: validatePass2,
            trigger: 'change',
          },
        ],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          api.register(this.ruleForm).then((res) => {
            if (res.data.code == 200) {
              this.$Message.success(res.data.data)
            } else {
              this.$Message.error(res.data.data)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },

    sendCode() {
      api.sendCode(this.ruleForm).then((res) => {
        this.$Message.success(res.data.data)
      })
    },
  },
}
</script>
<style>
.login {
  background-image: url('../image/login.png'); /* 替换为你的图片路径 */
  background-size: cover; /* 确保背景图覆盖整个元素 */
  background-repeat: no-repeat; /* 避免背景图片重复 */
  background-position: center center; /* 将背景图居中显示 */
  height: 100vh; /* 设置高度为视口高度 */
  display: flex; /* 使用 Flexbox 布局 */
  align-items: center; /* 垂直居中 */
  justify-content: center; /* 水平居中 */
}
.login-card {
  margin-top: -15%;
  width: 400px;
  background: #1a1e54;
  border: 0;
}

.input_label {
  color: #409eff;
}
</style>
